<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>注册界面</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="../CSS/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="../js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="../js/bootstrap.min.js"></script>

    <!--表单校验-->
    <script>

        // 校验用户名
        function checkUsername(){
            var username = $("#username").val();
            // 要求包含大写、小写字母，数字，特殊字符(@#$%^)的组合，不能低于6位
            var reg_username = /^(?![A-Za-z0-9]+$)(?![a-z0-9@#$%^]+$)(?![A-Za-z@#$%^]+$)(?![A-Z0-9@#$%^]+$)[a-zA-Z0-9@#$%^]{6,}$/;
            var test = reg_username.test(username);
            if (test){
                $("#username").css("border","");
            }else {
                $("#username").css("border","1px solid red");
            }
            return test;
        }

        // 校验密码
        function checkPassword(){
            var password = $("#password").val();
            // 密码8-20位[a-zA-Z0-9_]
            var reg_password = /^\w{8,20}$/;
            var test = reg_password.test(password);
            if (test){
                $("#password").css("border","");
            }else {
                $("#password").css("border","1px solid red");
            }
            return test;
        }

        // 入口函数
        $(function () {
            $("#username").blur(checkUsername);
            $("#password").blur(checkPassword);
            // 给注册按钮绑定单击事件
            $("#registerForm").submit(function () {
                if(checkUsername() && checkPassword()){
                    $.post("http://localhost:8080/news_war_exploded/registServlet",$(this).serialize(),function(data){
                        if(data.flag){
                            location.href="/news_war_exploded/admin/login.jsp";
                        }else{
                            $("#error").html(data.error);
                        }
                    });
                }else{
                    $("#error").html("用户名或密码格式错误！");
                }
                return false;
            });
        });
        </script>
</head>
<body>
<div class="container" style="width: 400px;">
    <h3 style="text-align: center;">管理员注册</h3>
    <form action="" method="post"  id="registerForm">
        <div class="form-group">
            <label for="username">用户名：</label>
            <input type="text" name="username" class="form-control" id="username" placeholder="请输入用户名"/>
        </div>

        <div class="form-group">
            <label for="password">密码：</label>
            <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/>
        </div>
        <hr/>
        <div class="form-group" style="text-align: center;">
            <input class="btn btn btn-primary" type="submit" value="注册" id="login">
        </div>
    </form>
    <!-- 提示的信息框 -->
    <div class="alert alert-warning alert-dismissible" role="alert" align="center">
        <button type="button" class="close" data-dismiss="alert" >
            <span>&times;</span>
        </button>
        <strong id="error">已有账号，<a href="login.jsp">立即登录</a><br></strong>
    </div>
</div>
</body>
</html>